const app = getApp()
var request = require('request')
function wxPromisify(fn) {
    return function (obj = {}) {
        return new Promise((resolve, reject) => {
            obj.success = function (res) {
                //成功
                resolve(res)
            }
            obj.fail = function (res) {
                //失败
                reject(res)
            }
            fn(obj)
        })
    }
}
//无论promise对象最后状态如何都会执行
Promise.prototype.finally = function (callback) {
    let P = this.constructor;
    return this.then(
        value => P.resolve(callback()).then(() => value),
        reason => P.resolve(callback()).then(() => { throw reason })
    );
};
/**
 * 微信用户登录,获取code
 */
function wxLogin() {
    return wxPromisify(wx.login)
}
/**
 * 获取微信用户信息
 * 注意:须在登录之后调用
 */
function wxGetUserInfo() {
    return wxPromisify(wx.getUserInfo)
}
/**
 * 获取系统信息
 */
function wxGetSystemInfo() {
    return wxPromisify(wx.getSystemInfo)
}

/**
 * 获取服务器登陆信息
 */
function kukeLoginToServer(code) {

    var url = app.globalData.kukeUrl + "/weixin/onLogin";
    var params = {
        js_code: code,
    }
    return request.getRequest(url,params)
}
/**
 * 获取收藏卡片信息
 */
function kukeGetRandomWork(workId) {

    var url = app.globalData.kukeUrl+"applet/random/work";
    var params = {
        userId: app.globalData.userId,
        workId:workId
    }
    return request.getRequest(url,params)
}
/**
 * 获取收藏卡片列表
 */
function kukeGetCardList(pageNoData,pageSizeData) {

    var url = app.globalData.kukeUrl+"applet/cardlist";
    var params = {
        userId:  app.globalData.userId,
        pageno:pageNoData,
        pageSize:pageSizeData,
    }
    return request.getRequest(url,params)
}
/**
 * 获取收藏卡片详情
 */
function kukeGetCardDetail(cardId) {

    var url = app.globalData.kukeUrl+"applet/card";
    var params = {
        cardId:cardId,
    }
    return request.getRequest(url,params)
}
/**
 * 设置是否喜欢
 */
function kukeSetFavoriteCollect(itemcode,workId,isLike) {

    var url = app.globalData.kukeUrl+"applet/favorite/collect";
    var params = {
        itemcode:itemcode,
        workId:workId,
        status:isLike,
        userId:app.globalData.userId,
    }
    return request.getRequest(url,params)
}
/**
 * 获取喜欢列表
 */
function kukeGetFavoriteList(pageNoData,pageSizeData) {
    var url = app.globalData.kukeUrl+"applet/favorite";
    var params = {
        userId:  app.globalData.userId,
        pageno:pageNoData,
        pageSize:pageSizeData,
    }
    return request.getRequest(url,params)
}
/**
 * 获取下一首歌曲
 */
function kukeGetNextMusic(lcode) {

    var url = "https://dc.kuke.com/v2/music/track/play";
    var params = {
        lcode: lcode,
        streamcode: '192'
    }
    return request.getRequest(url,params)
}

module.exports = {
    wxPromisify: wxPromisify,
    wxLogin: wxLogin,
    wxGetUserInfo: wxGetUserInfo,
    wxGetSystemInfo: wxGetSystemInfo,
    kukeLoginToServer:kukeLoginToServer,
    kukeGetCardList:kukeGetCardList,
    kukeGetRandomWork:kukeGetRandomWork,
    kukeSetFavoriteCollect:kukeSetFavoriteCollect,
    kukeGetFavoriteList:kukeGetFavoriteList,
    kukeGetNextMusic:kukeGetNextMusic,
    kukeGetCardDetail:kukeGetCardDetail,
}